<template>
  <div class="map-chart">
    <div ref="mapContainer" class="map-container"></div>
  </div>
</template>

<script>
import * as echarts from 'echarts';
import 'echarts/map/js/china.js';

export default {
  name: 'MapChart',
  props: {
    mapData: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      chart: null
    }
  },
  mounted() {
    this.initChart();
  },
  beforeUnmount() {
    if (this.chart) {
      this.chart.dispose();
    }
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$refs.mapContainer);
      this.updateChart();
    },
    updateChart() {
      if (!this.chart) return;
      
      const option = {
        title: {
          text: '地域分布图',
          left: 'center'
        },
        tooltip: {
          trigger: 'item',
          formatter: '{b}<br/>{c} (数量)'
        },
        visualMap: {
          min: 0,
          max: 1000,
          inRange: {
            color: ['#e0f3f8', '#3a68f2']
          },
          text: ['高', '低'],
          calculable: true
        },
        series: [
          {
            name: '地域分布',
            type: 'map',
            map: 'china',
            roam: false,
            label: {
              show: true
            },
            data: this.mapData
          }
        ]
      };
      
      this.chart.setOption(option);
    }
  },
  watch: {
    mapData: {
      handler() {
        this.updateChart();
      },
      deep: true
    }
  }
}
</script>

<style scoped>
.map-chart {
  width: 100%;
  height: 100%;
}

.map-container {
  width: 100%;
  height: 500px;
}
</style>